// radarPages/salesRank/salesRank.js
import {
  get_open_id,
  user_login,
  get_radar_list
} from "../../utils/api.js";
const app = getApp()
var page = 1;
var selectedNews = false;
var newsList = [];
const util = require('../../utils/util')
const {
  formatTimeC
} = util

Page({

  /**
   * 页面的初始数据
   */
  data: {
    SwitchId: 0,  // 0：月榜，1：总榜
    radarInfo: [],
    radarInfo2: [],
    rank1: {
      logo: '',
      username: '',
      user_sale_num:''
    },
    rank2: {
      logo: '',
      username: '',
      user_sale_num: ''
    },
    rank3: {
      logo: '',
      username: '',
      user_sale_num: ''
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRadarList(1);
  },

  // 获取排行榜列表
  getRadarList: function(type) {
    const that = this
    let url = '/user_api/rankingList';
    app.HttpPostSend(url, {
      type: type
    },
      function(res) {
        if (res.msg != "success") {
          app.showTipMsg(res.msg);
        } else {
          var radarInfo = res.data;
          var radarInfo2 = [];
          if (radarInfo.length > 0) {

            if (radarInfo.length <2 ) {
              var obj1 = {};
              if (radarInfo[0].username.length > 3) {
                radarInfo[0].username = radarInfo[0].username.slice(0, 3) + '...';
              }
              obj1.username = radarInfo[0].username;
              obj1.logo = radarInfo[0].logo;
              obj1.user_sale_num = radarInfo[0].user_sale_num;

              that.setData({
                rank1: obj1
              })

            } else if (radarInfo.length < 3) {

              var obj1 = {};
              if (radarInfo[0].username.length > 3) {
                radarInfo[0].username = radarInfo[0].username.slice(0, 3) + '...';
              }
              obj1.username = radarInfo[0].username;
              obj1.logo = radarInfo[0].logo;
              obj1.user_sale_num = radarInfo[0].user_sale_num;

              var obj2 = {};
              if (radarInfo[1].username.length > 3) {
                radarInfo[1].username = radarInfo[1].username.slice(0, 3) + '...';
              }
              obj2.username = radarInfo[1].username;
              obj2.logo = radarInfo[1].logo;
              obj2.user_sale_num = radarInfo[1].user_sale_num;

              that.setData({
                rank1: obj1,
                rank2: obj2
              })

            } else {

              var obj1 = {};
              if (radarInfo[0].username.length > 3) {
                radarInfo[0].username = radarInfo[0].username.slice(0, 3) + '...';
              }
              obj1.username = radarInfo[0].username;
              obj1.logo = radarInfo[0].logo;
              obj1.user_sale_num = radarInfo[0].user_sale_num;

              var obj2 = {};
              if (radarInfo[1].username.length > 3) {
                radarInfo[1].username = radarInfo[1].username.slice(0, 3) + '...';
              }
              obj2.username = radarInfo[1].username;
              obj2.logo = radarInfo[1].logo;
              obj2.user_sale_num = radarInfo[1].user_sale_num;

              var obj3 = {};
              if (radarInfo[2].username.length > 3) {
                radarInfo[2].username = radarInfo[2].username.slice(0, 3) + '...';
              }
              obj3.username = radarInfo[2].username;
              obj3.logo = radarInfo[2].logo;
              obj3.user_sale_num = radarInfo[2].user_sale_num;

              that.setData({
                rank1: obj1,
                rank2: obj2,
                rank3: obj3
              })

              for (var i = 3; i < radarInfo.length; i++) {
                radarInfo2.push(radarInfo[i]);
              }

            } 

          }
          that.setData({
            radarInfo2: radarInfo2
          })
          
        }
      })
  },

  // 头部Tab切换
  swichHeadTab: function (e) {
    const that = this
    const tap_switch_id = e.currentTarget.dataset.id
    const SwitchId = this.data.SwitchId
    if (tap_switch_id == SwitchId) {
      // console.log(true)
    } else {
      let change = new Object()
      switch (tap_switch_id) {
        case 0:
          change['SwitchId'] = tap_switch_id
          that.getRadarList(1)
          break;

        case 1:
          change['SwitchId'] = tap_switch_id
          that.getRadarList(2)
          break;

        default:
          break;
      }
      that.setData(change)
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

})